﻿using System;
using System.Collections;
using System.IO;
using Microsoft.Office.Interop.Excel;

namespace LocalisationResourceManager
{
    /// <summary>
    /// The class for the collection of Excel sheets, that is, the translation sheets.
    /// </summary>
    public class ResourceRepository : Hashtable
    {
        public Hashtable Initialize()
        {
            if (!Directory.Exists(Lrm.ResourceRepositoryPath))
                Directory.CreateDirectory(Lrm.ResourceRepositoryPath);

            System.Reflection.Missing missing = System.Reflection.Missing.Value;

            var oEa = new Application();
            Workbooks oWBs;

            var culturesFoundInXlsFiles = new Hashtable();

            // load all xls files existing in the resource repository folder.
            foreach (FileInfo fi in (new DirectoryInfo(Lrm.ResourceRepositoryPath)).GetFiles())
            {
                if (fi.Extension.ToLower() != ".xls")
                    continue;

                oWBs = oEa.Workbooks;
                // You may need to modify this method if you are not using Excel 11.0.
                oWBs.Open(fi.FullName, missing, true, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);

                _Workbook oWb = oWBs[1];

                foreach (Worksheet oSheet in oWb.Sheets)
                    if (!oSheet.Name.StartsWith("Sheet"))
                    {
                        string culture = oSheet.Name;

                        if (TranslationSheet.IsSupportdCultureInfo(System.Globalization.CultureTypes.AllCultures, culture))
                            culturesFoundInXlsFiles[culture] = culture;

                        Range oRange = oSheet.UsedRange;

                        var saRet = (object[,])oRange.Value2;

                        if (saRet == null)
                            continue;

                        int lb = saRet.GetLowerBound(0);
                        int ub = saRet.GetUpperBound(0);

                        // build the translation sheet.
                        TranslationSheet tSheet = this[culture];
                        tSheet.XlsFile = fi.FullName;

                        for (int i = lb; i <= ub; i++)
                        {
                            object value = saRet[i, 1];
                            string invariantString = (value == null) ? null : value.ToString();

                            if (invariantString == null)
                                continue;

                            value = saRet[i, 2];
                            string localizedValue = (value == null) ? null : value.ToString();

                            value = saRet[i, 3];
                            string name = (value == null) ? null : value.ToString();

                            value = saRet[i, 4];
                            string path = (value == null) ? null : value.ToString();

                            tSheet.Add(invariantString, path, name, localizedValue);
                        }
                    }

                oWb.Close(missing, missing, missing);
                oWBs.Close();
            }
            oEa.Quit();

            return culturesFoundInXlsFiles;
        }

        public TranslationSheet this[string culture]
        {
            get
            {
                if (!base.ContainsKey(culture))
                {
                    var tSheet = new TranslationSheet(culture);
                    this[culture] = tSheet;
                }

                return (TranslationSheet)base[culture];
            }
            set
            {
                base[culture] = value;
            }
        }

        public void Save()
        {
            foreach (DictionaryEntry de in this)
                ((TranslationSheet)de.Value).Save();
        }

        //  Use this to determine if it is needed to create new language-specific resource files.
        public DateTime CultureLastModifiedTime(string culture)
        {
            if (base.ContainsKey(culture))
            {
                return ((TranslationSheet)base[culture]).LastModified;
            }
            
            return DateTime.Now;
        }
    }
}
